package com.net.oil.coupon.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.coupon.CouponDetailActivity;
import com.net.oil.coupon.bean.CouponRecordRefBusiness;
import com.net.oil.coupon.bean.CouponRecordVO;
import com.net.oil.util.DateFormat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/4/29 0029:下午 15:46
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class CouponListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;

    private List<CouponRecordVO> couponBeans;

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10
    private int used;          //使用状态(1:已使用 0：未使用 -1：已过期)

    public CouponListAdapter(Context context,int used) {
        this.context = context;
        this.used = used;
        inflater = LayoutInflater.from(context);
        couponBeans = new ArrayList<>();
    }

    public void updateData(List<CouponRecordVO> couponBeans) {
        this.couponBeans.clear();
        this.couponBeans.addAll(couponBeans);
        notifyDataSetChanged();
    }

    public void loadMore(List<CouponRecordVO> couponBeans) {
        this.couponBeans.addAll(couponBeans);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.coupon_list_item, parent, false);
                return new ViewHolder(view);
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            default:
                return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            initItemView(viewHolder, position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }
    }

    private void initItemView(final ViewHolder viewHolder, int position) {
        final CouponRecordVO couponBean = couponBeans.get(position);

        /* 优惠券时间 */
        String time = DateFormat.formatDateTime(couponBean.getApplyTime(), DateFormat.FormatFlag.YDM) + "至" +
                DateFormat.formatDateTime(couponBean.getExpireTime(), DateFormat.FormatFlag.YDM);
        viewHolder.time.setText(time);

        /* 优惠券名称 */
        if (TextUtils.isEmpty(couponBean.getCouponName())){
            viewHolder.couponTitle.setText("优惠券");
        }else {
            viewHolder.couponTitle.setText(couponBean.getCouponName());
        }

        /* 优惠券金额 */
        viewHolder.amount.setText(String.format(context.getString(R.string.money_format), String.valueOf(couponBean.getAmount())));
        if (couponBean.getUsed() != null && couponBean.getUsed()){
            viewHolder.amount.setTextColor(context.getResources().getColor(R.color.text_grey));
            viewHolder.couponBgLayout.setBackground(context.getResources().getDrawable(R.drawable.coupon_bg_use));
        }

        if (couponBean.getExpireStatus() == 1){     //已过期
            viewHolder.amount.setTextColor(context.getResources().getColor(R.color.text_grey));
            viewHolder.couponBgLayout.setBackground(context.getResources().getDrawable(R.drawable.coupon_bg_use));
        }

        /* 使用限制：优惠券满多少元可用，如果字段为空就不展示 */
        Double fullAvailableAmount = couponBean.getFullAvailableAmount();
        if (fullAvailableAmount != null){
            viewHolder.useRestrictTv.setText(String.format(context.getString(R.string.coupon_use_format), String.valueOf(fullAvailableAmount)));
        }

        /* 可使用的商家 */
        List<CouponRecordRefBusiness> list = couponBean.getCouponRecordRefBusinessList();
        if (list != null){
            String names = null;
            for (CouponRecordRefBusiness business : list){
                if (!TextUtils.isEmpty(business.getBusinessName())){
                    if (TextUtils.isEmpty(names)){
                        names = business.getBusinessName();
                    }else {
                        names = names + business.getBusinessName() + "，";
                    }
                }
            }
            viewHolder.content.setText(names);
        }else {
            viewHolder.content.setText("全部商家可用");
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CouponDetailActivity.class);
                intent.putExtra(Constant.INTENT_COUPON_ID_KEY, couponBean.getId());
                context.startActivity(intent);
            }
        });

        viewHolder.unfoldImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.content.getMaxLines() == 1){
                    viewHolder.content.setMaxLines(Integer.MAX_VALUE);
                    viewHolder.unfoldImg.setRotation(-90);
                }else {
                    viewHolder.content.setMaxLines(1);
                    viewHolder.unfoldImg.setRotation(90);
                }

            }
        });

    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return couponBeans == null ? 0 : couponBeans.size() + 1;
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.amount)
        TextView amount;
        @BindView(R.id.use_restrict_tv)
        TextView useRestrictTv;
        @BindView(R.id.coupon_bg_layout)
        FrameLayout couponBgLayout;
        @BindView(R.id.coupon_title)
        TextView couponTitle;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.unfold_img)
        ImageView unfoldImg;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
